pro camera2hdf5,infile,outfile,eigenvec=eigenvec,velocity=velocity

if (n_params() lt 2 or n_params() gt 3) then begin
   print,' usage: camera2hdf5 infile outfile [eigenvec,velocity]'
   stop
end

print,''
print,' ----------------------------------------'
print,' camera2hdf5 (2008)'
print,' Rob Crain, ICC, Durham'
print,' Part of Typhoon by Rob Crain & Jim Geach'
print,' ----------------------------------------'
print,''

if keyword_set(eigenvec)then begin
   use_eigenvec=1
   print,' Using eigenvector orientation'
endif else begin
   use_eigenvec = 0
   print,' Using angular orientation'
endelse

if keyword_set(velocity)then begin
   use_velocity=1
   print,' Including bulk peculiar velocity'
endif else use_velocity = 0

;read flightpath
numin  = 0l
numout = 0l
openr,1,infile
readf,1,numin,numout
print,' Found '+strtrim(string(numin),2)    +' input times'

; allocate storage
t        = lonarr(numin)
x        = fltarr(numin)
y        = fltarr(numin)
z        = fltarr(numin)
Lx       = fltarr(numin)
Ly       = fltarr(numin)
Lz       = fltarr(numin)
in       = create_struct('t',t,'x',x,'y',y,'z',z,'Lx',Lx,'Ly',Ly,'Lz',Lz)
tab      = create_struct('t',t,'x',x,'y',y,'z',z,'Lx',Lx,'Ly',Ly,'Lz',Lz)
if(use_eigenvec eq 0)then begin
   theta = fltarr(numin)
   phi   = fltarr(numin)
   psi   = fltarr(numin)
   in    = create_struct(in ,'theta',theta,'phi',phi,'psi',psi)
   tab   = create_struct(tab,'theta',theta,'phi',phi,'psi',psi)
endif else begin
   xpri  = dblarr(3,numin)
   ypri  = dblarr(3,numin)
   zpri  = dblarr(3,numin)   
   in    = create_struct(in ,'xpri',xpri,'ypri',ypri,'zpri',zpri)
   tab   = create_struct(tab,'xpri',xpri,'ypri',ypri,'zpri',zpri)
endelse
if(use_velocity eq 1)then begin
   vx    = fltarr(numin)
   vy    = fltarr(numin)
   vz    = fltarr(numin)
   in    = create_struct(in ,'vx',vx,'vy',vy,'vz',vz)
   tab   = create_struct(tab,'vx',vx,'vy',vy,'vz',vz)
endif 

undefine,t  
undefine,x  
undefine,y  
undefine,z  
undefine,Lx 
undefine,Ly 
undefine,Lz
if(use_eigenvec eq 0)then begin
   undefine,theta
   undefine,phi   
   undefine,psi   
endif else begin
   undefine,xpri
   undefine,ypri
   undefine,zpri 
endelse
if(use_velocity eq 1)then begin
   undefine,vx
   undefine,vy
   undefine,vz
endif 

for i=0,numin-1 do begin
   if(use_eigenvec eq 0)then begin
       if(use_velocity eq 0)then begin
           readf,1,t,x,y,z,Lx,Ly,Lz,theta,phi,psi
           in.t[i]      = t
           in.x[i]      = x
           in.y[i]      = y
           in.z[i]      = z
           in.Lx[i]     = Lx
           in.Ly[i]     = Ly
           in.Lz[i]     = Lz
           in.theta[i]  = theta
           in.phi[i]    = phi
           in.psi[i]    = psi
       endif else begin
           readf,1,t,x,y,z,vx,vy,vz,Lx,Ly,Lz,theta,phi,psi
           in.t[i]      = t
           in.x[i]      = x
           in.y[i]      = y
           in.z[i]      = z
           in.vx[i]     = vx
           in.vy[i]     = vy
           in.vz[i]     = vz
           in.Lx[i]     = Lx
           in.Ly[i]     = Ly
           in.Lz[i]     = Lz
           in.theta[i]  = theta
           in.phi[i]    = phi
           in.psi[i]    = psi
       endelse
   endif else begin
       if(use_velocity eq 0)then begin
           readf,1,t,x,y,z,Lx,Ly,Lz,xpri_x,xpri_y,xpri_z,ypri_x,ypri_y,ypri_z,zpri_x,zpri_y,zpri_z
           in.t[i]      = t
           in.x[i]      = x
           in.y[i]      = y
           in.z[i]      = z
           in.Lx[i]     = Lx
           in.Ly[i]     = Ly
           in.Lz[i]     = Lz
           in.xpri[0,i] = xpri_x
           in.xpri[1,i] = xpri_y
           in.xpri[2,i] = xpri_z
           in.ypri[0,i] = ypri_x
           in.ypri[1,i] = ypri_y
           in.ypri[2,i] = ypri_z
           in.zpri[0,i] = zpri_x
           in.zpri[1,i] = zpri_y
           in.zpri[2,i] = zpri_z
       endif else begin
           readf,1,t,x,y,z,vx,vy,vz,Lx,Ly,Lz,xpri_x,xpri_y,xpri_z,ypri_x,ypri_y,ypri_z,zpri_x,zpri_y,zpri_z
           in.t[i]      = t
           in.x[i]      = x
           in.y[i]      = y
           in.z[i]      = z
           in.vx[i]     = vx
           in.vy[i]     = vy
           in.vz[i]     = vz
           in.Lx[i]     = Lx
           in.Ly[i]     = Ly
           in.Lz[i]     = Lz
           in.xpri[0,i] = xpri_x
           in.xpri[1,i] = xpri_y
           in.xpri[2,i] = xpri_z
           in.ypri[0,i] = ypri_x
           in.ypri[1,i] = ypri_y
           in.ypri[2,i] = ypri_z
           in.zpri[0,i] = zpri_x
           in.zpri[1,i] = zpri_y
           in.zpri[2,i] = zpri_z
       endelse
   endelse
endfor
close,1

print,in.Lx

print,' Interpolating over '+strtrim(string(numout),2)+' output times'

; allocate output storage
t        = lonarr(numout)
x        = fltarr(numout)
y        = fltarr(numout)
z        = fltarr(numout)
Lx       = fltarr(numout)
Ly       = fltarr(numout)
Lz       = fltarr(numout)
out      = create_struct('t',t,'x',x,'y',y,'z',z,'Lx',Lx,'Ly',Ly,'Lz',Lz)
if(use_eigenvec eq 0)then begin
   theta = fltarr(numout)
   phi   = fltarr(numout)
   psi   = fltarr(numout)
   out   = create_struct(out,'theta',theta,'phi',phi,'psi',psi)
endif else begin
   xpri  = dblarr(3,numout)
   ypri  = dblarr(3,numout)
   zpri  = dblarr(3,numout)   
   out   = create_struct(out,'xpri',xpri,'ypri',ypri,'zpri',zpri)
endelse
if(use_velocity eq 1)then begin
   vx    = fltarr(numout)
   vy    = fltarr(numout)
   vz    = fltarr(numout)    
   out   = create_struct(out,'vx',vx,'vy',vy,'vz',vz)
endif

print,' -tabulating spline coefficients'

tab.x   = spl_init(in.t,in.x ,/double)
tab.y   = spl_init(in.t,in.y ,/double)
tab.z   = spl_init(in.t,in.z ,/double)
tab.Lx  = spl_init(in.t,in.Lx,/double)
tab.Ly  = spl_init(in.t,in.Ly,/double)
tab.Lz  = spl_init(in.t,in.Lz,/double)
if(use_eigenvec eq 0)then begin
   tab.theta = spl_init(in.t,in.theta,/double)
   tab.phi   = spl_init(in.t,in.phi  ,/double)
   tab.psi   = spl_init(in.t,in.psi  ,/double)
endif else begin
   tab.xpri[0,*]  = spl_init(in.t,in.xpri[0,*],/double)
   tab.xpri[1,*]  = spl_init(in.t,in.xpri[1,*],/double)
   tab.xpri[2,*]  = spl_init(in.t,in.xpri[2,*],/double)
   tab.ypri[0,*]  = spl_init(in.t,in.ypri[0,*],/double)
   tab.ypri[1,*]  = spl_init(in.t,in.ypri[1,*],/double)
   tab.ypri[2,*]  = spl_init(in.t,in.ypri[2,*],/double)
   tab.zpri[0,*]  = spl_init(in.t,in.zpri[0,*],/double)
   tab.zpri[1,*]  = spl_init(in.t,in.zpri[1,*],/double)
   tab.zpri[2,*]  = spl_init(in.t,in.zpri[2,*],/double)
endelse
if(use_velocity eq 1)then begin
    tab.vx = spl_init(in.t,in.vx,/double)
    tab.vy = spl_init(in.t,in.vy,/double)
    tab.vz = spl_init(in.t,in.vx,/double)
endif

print,' -running splines'

for i=0,numout-1 do begin

   out.t[i]  = i+in.t[0]
   out.x[i]  = spl_interp(in.t,in.x ,tab.x ,out.t[i])
   out.y[i]  = spl_interp(in.t,in.y ,tab.y ,out.t[i])
   out.z[i]  = spl_interp(in.t,in.z ,tab.z ,out.t[i])
   out.Lx[i] = spl_interp(in.t,in.Lx,tab.Lx,out.t[i])
   out.Ly[i] = spl_interp(in.t,in.Ly,tab.Ly,out.t[i])
   out.Lz[i] = spl_interp(in.t,in.Lz,tab.Lz,out.t[i])
   if(use_eigenvec eq 0)then begin
      out.theta[i] = spl_interp(in.t,in.theta,tab.theta,out.t[i])
      out.phi[i]   = spl_interp(in.t,in.phi  ,tab.phi  ,out.t[i])
      out.psi[i]   = spl_interp(in.t,in.psi  ,tab.psi  ,out.t[i])
   endif else begin
      out.xpri[0,i] = spl_interp(in.t,in.xpri[0,*],tab.xpri[0,*],out.t[i])
      out.xpri[1,i] = spl_interp(in.t,in.xpri[1,*],tab.xpri[1,*],out.t[i])
      out.xpri[2,i] = spl_interp(in.t,in.xpri[2,*],tab.xpri[2,*],out.t[i])
      out.ypri[0,i] = spl_interp(in.t,in.ypri[0,*],tab.ypri[0,*],out.t[i])
      out.ypri[1,i] = spl_interp(in.t,in.ypri[1,*],tab.ypri[1,*],out.t[i])
      out.ypri[2,i] = spl_interp(in.t,in.ypri[2,*],tab.ypri[2,*],out.t[i])
      out.zpri[0,i] = spl_interp(in.t,in.zpri[0,*],tab.zpri[0,*],out.t[i])
      out.zpri[1,i] = spl_interp(in.t,in.zpri[1,*],tab.zpri[1,*],out.t[i])
      out.zpri[2,i] = spl_interp(in.t,in.zpri[2,*],tab.zpri[2,*],out.t[i])
  endelse
  if(use_velocity eq 1)then begin
      out.vx[i] = spl_interp(in.t,in.vx,tab.vx,out.t[i])
      out.vy[i] = spl_interp(in.t,in.vy,tab.vy,out.t[i])
      out.vz[i] = spl_interp(in.t,in.vz,tab.vz,out.t[i])
  endif
endfor

print,' Writing camera file'
file_id = h5f_create(outfile)
grp_id  = h5g_create(file_id,'Flightpath')

datatype_id  = h5t_idl_create(numout)
dataspace_id = h5s_create_scalar()
attr_id      = h5a_create(grp_id,'NumFrames',datatype_id,dataspace_id)
h5a_write,attr_id,numout

datatype_id  = h5t_idl_create(out.t)
dataspace_id = h5s_create_simple(size(out.t,/dimen))
dataset_id   = h5d_create(grp_id,'Frames',datatype_id,dataspace_id)
h5d_write,dataset_id,out.t

datatype_id  = h5t_idl_create(out.x)
dataspace_id = h5s_create_simple(size(out.x,/dimen))
dataset_id   = h5d_create(grp_id,'X-coordinates',datatype_id,dataspace_id)
h5d_write,dataset_id,out.x
dataset_id   = h5d_create(grp_id,'Y-coordinates',datatype_id,dataspace_id)
h5d_write,dataset_id,out.y
dataset_id   = h5d_create(grp_id,'Z-coordinates',datatype_id,dataspace_id)
h5d_write,dataset_id,out.z
dataset_id   = h5d_create(grp_id,'Lx',datatype_id,dataspace_id)
h5d_write,dataset_id,out.Lx
dataset_id   = h5d_create(grp_id,'Ly',datatype_id,dataspace_id)
h5d_write,dataset_id,out.Ly
dataset_id   = h5d_create(grp_id,'Lz',datatype_id,dataspace_id)
h5d_write,dataset_id,out.Lz

if(use_eigenvec eq 0)then begin
   datatype_id  = h5t_idl_create(out.theta)
   dataspace_id = h5s_create_simple(size(out.theta,/dimen))
   dataset_id   = h5d_create(grp_id,'Theta',datatype_id,dataspace_id)
   h5d_write,dataset_id,out.theta
   dataset_id   = h5d_create(grp_id,'Phi',datatype_id,dataspace_id)
   h5d_write,dataset_id,out.phi
   dataset_id   = h5d_create(grp_id,'Psi',datatype_id,dataspace_id)
   h5d_write,dataset_id,out.psi
endif else begin
   datatype_id  = h5t_idl_create(out.xpri)
   dataspace_id = h5s_create_simple(size(out.xpri,/dimen))
   dataset_id   = h5d_create(grp_id,'X-Prime',datatype_id,dataspace_id)
   h5d_write,dataset_id,out.xpri
   dataset_id   = h5d_create(grp_id,'Y-Prime',datatype_id,dataspace_id)
   h5d_write,dataset_id,out.ypri
   dataset_id   = h5d_create(grp_id,'Z-Prime',datatype_id,dataspace_id)
   h5d_write,dataset_id,out.zpri
endelse
if(use_velocity eq 1)then begin
   datatype_id  = h5t_idl_create(out.vx)
   dataspace_id = h5s_create_simple(size(out.vx,/dimen))
   dataset_id   = h5d_create(grp_id,'X-velocity',datatype_id,dataspace_id)
   h5d_write,dataset_id,out.vx
   dataset_id   = h5d_create(grp_id,'Y-velocity',datatype_id,dataspace_id)
   h5d_write,dataset_id,out.vy
   dataset_id   = h5d_create(grp_id,'Z-velocity',datatype_id,dataspace_id)
   h5d_write,dataset_id,out.vz
endif

h5f_close,file_id

print,' Done'
print,''

end
